package com.linian.pushforcampus.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linian.pushforcampus.domain.PushConfigure;
import com.linian.pushforcampus.domain.User;
import com.linian.pushforcampus.domain.WechatAccessToken;
import com.linian.pushforcampus.mapper.UserMapper;
import com.linian.pushforcampus.service.UserService;
import com.linian.pushforcampus.service.WechatAccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.linian.pushforcampus.util.HttpUtil.getHttpJsonByUrl;

/**
 * packageName com.linian.pushforcampus.service.Impl
 *
 * @author Lennon
 * @className UserServiceImpl
 * @date 2023/11/12
 * @description TODO
 */

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WechatAccessTokenService wechatAccessTokenService;


    @Autowired
    private PushConfigure pushConfigure;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public List<User> getAllUsersByUser(User user) {



        return userMapper.getAllUsersByUser(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public User getUserByCode(String code) {
        return userMapper.getUserByCode(code);
    }

    @Override
    public void insertUser(User user) {

        userMapper.insertUser(user);

    }

    @Override
    public List<User> getUserByIdWhoState1() {
        return userMapper.getUserByIdWhoState1();
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public List<User> getNowUserByWechat() {
        //设置当前时间
        LocalDateTime now = LocalDateTime.now();

        //创建最后需要发送的List
        List<User> userListByWX = new ArrayList<>();
        //查询当前有效的accessToken
        WechatAccessToken wechatAccessTokenById = wechatAccessTokenService.getWechatAccessTokenById(1);
        String accessToken = wechatAccessTokenById.getAccessToken();
        //通过token 获取当前公主号关注的的用户表

        String userListJson = getHttpJsonByUrl(pushConfigure.getWECHARUSERLISTURL().replace("ACCESS_TOKEN", accessToken));
        JSONObject jsonObject = JSONObject.parseObject(userListJson);
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("openid");
        //解析好后的json，转换为list对象
        for (int i = 0; i < jsonArray.size(); i++) {

            User user = new User();
            user.setUserId(jsonArray.getString(i));
            user.setDataState(1);
            user.setCreateTime(now);
            user.setUpdateTime(now);
            userListByWX.add(user);
        }




        return userListByWX;
    }


}
